package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.UseCaseDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.UseCaseDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/10 20:16
 */
@Service("useCaseDAO")
public class MongoUseCaseDAO extends AbstractBaseDAO<UseCaseDO> implements UseCaseDAO {
    @Override
    public void insert(UseCaseDO useCaseDO) {
        super.insert(useCaseDO);
        mongoTemplate.insert(useCaseDO, COLLECTION_NAME);
    }

    @Override
    public void update(UseCaseDO useCaseDO) {
        super.update(useCaseDO);
        mongoTemplate.save(useCaseDO, COLLECTION_NAME);
    }

    @Override
    public List<UseCaseDO> queryAll() {
        Criteria criteria = Criteria.where(UseCaseColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<UseCaseDO> pageQuery(UseCaseDO useCaseDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(UseCaseColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(useCaseDO.getUseCaseName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(UseCaseColumn.USE_CASE_NAME).regex("^.*" + useCaseDO.getUseCaseName() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(useCaseDO.getUseCaseKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(UseCaseColumn.USE_CASE_KEY).regex("^.*" + useCaseDO.getUseCaseKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(useCaseDO.getSystemUuid())) {
            criteria.and(UseCaseColumn.SYSTEM_UUID).is(useCaseDO.getSystemUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, UseCaseColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public UseCaseDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(UseCaseColumn._ID).is(_id);
        criteria.and(UseCaseColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return UseCaseColumn.class;
    }

    @Override
    public UseCaseDO queryBySystemUuidAndName(String systemUuid, String name) {
        Criteria criteria = Criteria.where(UseCaseColumn.USE_CASE_NAME).is(name);
        criteria.and(UseCaseColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UseCaseColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public UseCaseDO queryBySystemUuidAndKey(String systemUuid, String key) {
        Criteria criteria = Criteria.where(UseCaseColumn.USE_CASE_KEY).is(key);
        criteria.and(UseCaseColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UseCaseColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override public UseCaseDO queryBySystemAndUseCase(String systemUuid, String useCaseUuid) {
        Criteria criteria = Criteria.where(UseCaseColumn.UUID).is(useCaseUuid);
        criteria.and(UseCaseColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UseCaseColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
